package com.example.tb.controller;

import com.example.tb.entity.User;
import com.example.tb.service.UserService;
import com.example.tb.utils.ResponseResult;
import com.example.tb.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/** 
* @Description: 登录 
* @Author: yaodada
* @Date: 2024/1/4 
*/ 

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/login")
    public ResponseResult<UserVo> loginController(@RequestParam String uname, @RequestParam String password){
        User user = userService.loginSer(uname, password);
        UserVo userVo = new UserVo();
        if(user!=null){
            userVo.setUid(user.getUid());
            userVo.setUname(user.getUname());
            userVo.setPassword(user.getPassword());
            userVo.setRid(userService.roleByUser(uname, password));
            log.info("用户  " + user.getUname() +"  登录成功  success");
            return ResponseResult.success("登陆成功",userVo);
        }else{
            log.info("账号或密码错误  error");
            return ResponseResult.error("账号或密码错误");
        }
    }

    /**
    * @Description: 注册权限校验
    * @Author: yaodada
    * @Date: 2023/12/25
    */
    @GetMapping("/registerAuthority")
    public ResponseResult<User> registerAuthorityController(@RequestParam String uname, @RequestParam String password){
        String i = userService.roleByUser(uname, password);
            if (("1").equals(i) || ("2").equals(i)){
            log.info("跳转注册  success");
            return ResponseResult.success("跳转注册");
        }else{
            log.info("注册权限不足  error");
            return ResponseResult.error("权限不足");
        }
    }

    @GetMapping("/register")
    public ResponseResult<User> registerController(@RequestParam String uname, @RequestParam String password,@RequestParam String roleId){
        User user = userService.registerSer(uname,password,roleId);
        if(user!=null){
            log.info("注册成功  success");
            return ResponseResult.success("注册成功",user);
        }else{
            log.info("用户名已存在  error");
            return ResponseResult.error("用户名已存在");
        }
    }
}
